package hotLeetcode;

import java.util.*;

public class H56Leetcode {
    public static int[][] merge(int[][] intervals) {
        for (int[] num : intervals) {
            Arrays.sort(num);

        }

        Arrays.sort(intervals, Comparator.comparing(a -> a[0]));


        List<int[]> list = new ArrayList<>();

        for (int[] p : intervals) {
            int m = list.size();
            if (m > 0 && list.get(m - 1)[1] >= p[0]) {
                list.get(m - 1)[1] =Math.max(list.get(m - 1)[1],p[1]);
            } else {
                list.add(p);
            }
        }


        return list.toArray(new int[list.size()][]);


    }

    public static void main(String[] args) {
        int[][] nums = {{1, 3}, {6, 2}, {15, 18}, {10, 8}};
//        int[][] nums = {{1, 4}, {2,3} };
        Arrays.sort(nums, (p, q) -> p[0] - q[0]);

//        int[][] merge = H56Leetcode.merge(nums);
        for (int[] ints : nums) {
            System.out.println(Arrays.toString(ints));
        }


    }

}
